package com.ng.common.utils;

import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cb
 * @Description TODO
 * @date 2023/3/2
 */
public class ImportTxtUtil {
    int idx;

    Connection conn = null;

    PreparedStatement pstmt = null;

    PreparedStatement pstmt1 = null;

    public Map readTxtFile() {

//存放内容的map对象

        Map filemaps = new HashMap();

        try {

// 本地环境路径

            String str1 = "D:\\TPIDataBase\\CJFX7911_02.txt";
            String outstr = "D:\\TPIDataBase\\sql.txt";
            String   CJFX7911="E:\\22\\CJFX7911.txt";
            String   CJFX791101="E:\\22\\CJFX7911_01.txt";

// 设定文件编码格式

            String encoding = "GB2312";

            File file = new File(CJFX791101);

// 启动数据库



            int count = 1;//定义章节顺序变量
            int  i=0;

            if (file.isFile() && file.exists()) { // 判断文件是否存在

                InputStreamReader read = new InputStreamReader(

                        new FileInputStream(file),encoding);// 考虑到编码格式

                BufferedReader bufferedReader = new BufferedReader(read);

                String lineTxt = null;

                String content = "";
                dbConnection();
                while ((lineTxt = bufferedReader.readLine()) != null) {//按行读取

                    if(!"".equals(lineTxt) ){
                        String reds="";
                        String reds1="";
                        if(lineTxt.contains("=")) {
                        String[]  vv= lineTxt.split("=");
                        if(vv.length==2){
                            reds =vv[0].replaceAll(" ","").
                                    replaceAll("<","").
                                    replaceAll(">","")
                            ; //对行的内容进行分析处理后再放入map里。
                            reds1 =vv[1].replaceAll("\"","\'");//对行的内容进行分析处理后再放入map里。
                            filemaps.put(reds,reds1);
                        }
//                        else if(vv.length==1){
//                            reds =vv[0].replaceAll(" ","").
//                                    replaceAll("<","").
//                                    replaceAll(">","");//对行的内容进行分析处理后再放入map里。
//                            reds1="";
//                        }



                        }

                        if(lineTxt.contains("REC")){

                            if(filemaps.size()>0){

                               // BufferedWriter out = new BufferedWriter(new FileWriter(writename));
                               String sql="insert into CJFX7911_01 ( ";
                                StringBuffer sb = new StringBuffer();
                                StringBuffer sbv = new StringBuffer();
                                for (Object key:filemaps.keySet()) {
                                    sb.append(key).append(",");
                                    sbv.append('"');
                                    sbv.append(filemaps.get(key)).append('"').append(",");
                                }
                                String str = sb.substring(0,sb.length()-1);
                                String  str2=sbv.substring(0,sbv.length()-1);
                                sql=sql+str+" ) VALUES ("+str2+")";

                                System.out.println(sql);
                                i++;
                                insertCustInfo(sql);
                                System.out.println("写入数据库 i="+i);
                                filemaps.clear();

                            }
                        }
                        /* 正则表达式处理标题名称 */

                        Pattern p = null;

                        Matcher mReds = null;






                    }

                }
                read.close();//关闭InputStreamReader

                bufferedReader.close();//关闭BufferedReader

            } else {

                System.out.println("找不到指定的文件");

            }

        } catch (Exception e) {

            System.out.println("读取文件内容出错");

            e.printStackTrace();

        }

        return filemaps;

    }

// 插入到数据库中

    public void insertCustInfo(String sql) throws IOException {

        try {
            Statement stmt=null;
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);

        } catch (Exception e) {
            String outstr = "D:\\TPIDataBase\\sql.txt";
            e.printStackTrace();

            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outstr));
            bufferedWriter.write(e.getMessage());
        }

    }

// 连接数据库

    public Connection dbConnection() {

        try {

            Class.forName("com.mysql.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/ng_server2";

            String user = "root";

            String password = "123456";

            conn = DriverManager.getConnection(url, user, password);

            System.out.println("Connection 开启！");

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

        }

        return conn;

    }

// 关闭数据库

    public void dbDisConnection() {

        if (conn != null) {

            try {

                conn.close();

                System.out.println("Connection 关闭！");

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }



}
